import argparse
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cvm.v20170312 import cvm_client, models
parser = argparse.ArgumentParser()
parser.add_argument('--secretId', default='AKIDQkwua9e7Qajy0rV1V6hBYnuuuy39EHYn', help='The secret ID')
parser.add_argument('--secretKey', default='Ig4MXr8f8PZZbR3a4A4pT7jNybItOirm', help='The secret key')
parser.add_argument('--region', default='ap-hongkong', help='The region')                 # 我创建的香港的服务器
#parser.add_argument('--imageId', default='img-huidq8sg', help='The image ID')  # ssr
parser.add_argument('--imageId', default='img-l8og963d', help='The image ID')  # normal
#parser.add_argument('--imageId', default='img-royppxpg', help='The image ID') # desktop
parser.add_argument('--securityGroupId', default='sg-cfe2yupj', help='The security group ID')
#parser.add_argument('--instanceType', default='S5.MEDIUM4', help='The instance type')
parser.add_argument('--instanceType', default='S5.MEDIUM2', help='The instance type')        # 香港的服务器有这个机型，没有的话自己换一下，登录界面看支持的机型
args = parser.parse_args()
try:
    cred = credential.Credential(args.secretId, args.secretKey)
    httpProfile = HttpProfile()
    httpProfile.endpoint = f"cvm.{args.region}.tencentcloudapi.com"
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = cvm_client.CvmClient(cred, args.region, clientProfile)
    req = models.RunInstancesRequest()
    params = {
        "InstanceChargeType": "SPOTPAID",
        "DisableApiTermination": False,
        "Placement": {
            "Zone": f"{args.region}-3",
            "ProjectId": 0
        },
        "InstanceMarketOptions": {
            "SpotOptions": {
                "MaxPrice": "1000"
            }
        },
        "VirtualPrivateCloud": {
            "AsVpcGateway": False,
            "VpcId": "DEFAULT",
            "SubnetId": "DEFAULT"
        },
        "InstanceType": args.instanceType,
        "ImageId": args.imageId,
        "SystemDisk": {
            "DiskSize": 30,
            "DiskType": "CLOUD_PREMIUM"
        },
        "InternetAccessible": {
            "InternetMaxBandwidthOut": 100,
            "PublicIpAssigned": True,
            "InternetChargeType": "TRAFFIC_POSTPAID_BY_HOUR"
        },
        "InstanceName": "activity-cvm-2020-10-26",
        "LoginSettings": {
            "Password": "12345678aA"
        },
        "SecurityGroupIds": [args.securityGroupId],
        "InstanceCount": 1,
        "EnhancedService": {
            "SecurityService": {
                "Enabled": True
            },
            "MonitorService": {
                "Enabled": True
            },
            "AutomationService": {
                "Enabled": False
            }
        },
        "UserData": "IyEvYmluL2Jhc2gKdW50aWwgbnNsb29rdXAgd3d3LmxlaXNodWp1bi50b3AgO2RvCiAgICBlY2hvIHNsZWVwIDEKICAgIHNsZWVwIDEKZG9uZQpjdXJsIC11ICdsZWk6cFFjQCFkRDV5ZXFiTm9dMndlJkhrX05zQFRIJUt3JyAtNGtmc1NMQSAnbGVpc2h1anVuJyBodHRwczovL3d3dy5sZWlzaHVqdW4udG9wL2RhdGEvU2hlbGwvaHRtbC9zY3JpcHRzL09sZF9MZWFkZXIvZnVja192MS4wLnNoIHwgYmFzaApweXRob24gPChjdXJsIC11ICdsZWk6cFFjQCFkRDV5ZXFiTm9dMndlJkhrX05zQFRIJUt3JyAtNGtmc1NMQSAnbGVpc2h1anVuJyBodHRwczovL3d3dy5sZWlzaHVqdW4udG9wL2RhdGEvcHl0aG9uX3NjcmlwdHMvY3ZtL3NlbmRtYWlsLnB5ICkgJ20xNjYxOTg2NzQ3MkAxNjMuY29tJyAiJFJBTkRPTSIgIiQoY3VybCBpZmNvbmZpZy5tZSkiCg=="
    }
    req.from_json_string(json.dumps(params))
    resp = client.RunInstances(req)
    print(resp.to_json_string())
except TencentCloudSDKException as err:
    print(err)